﻿using CustomModel;
using IDeskService;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utility;

namespace DeskInterfaceService
{
    /// <summary>
    /// 用户登陆信息
    /// </summary>
   public  class AuthService:QuickPostBase,IAuthService
    {
        
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="username">用户登录名，nicename</param>
        /// <param name="pwd">用户密码，这里的密码是已经加密过的</param>
        /// <param name="error">返回处理的结果</param>
        /// <returns>成功返回实体，否则返回null</returns>
       public bool GetToken(string username,string pwd,out string error)
        {
           
     //       string url = CommonHelper.BaseUrl + "Demo/DemoTest";
            Desk_LoginModel loginModel = new Desk_LoginModel();
            loginModel.UserName = username;
            loginModel.Pwd = pwd;
            this.UseAuth = false;
            //发送
            this.param.Add("login", Model_JsonConverter<Desk_LoginModel>.ModelToJson(loginModel));
            this.BaseUrl = "http://localhost:8701/";
            this.ControllerAction = "token"; //验证控制器
             string res =  this.PostData();
            TokenModel tm = Model_JsonConverter<TokenModel>.JsonToModel(res);
            if (tm==null)
            {
                error = "未成功获取验证";
                return false;
            }
            CommonHelper.DeskToken = tm.access_token;
            error = null;
            return true;
         
  
        }

        /// <summary>
        /// 登陆，通过用户手机号码和密码进行登陆，返回用户实体
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public pet_user Login(string name,string pwd,out string error)
        {
            this.ControllerAction = "user/getuser";
            this.UseAuth = true;
            this.param.Add("phone", name);
            this.param.Add("pwd", pwd);
            string res = this.PostData();
            JsonModel<pet_user> model = Model_JsonConverter<JsonModel<pet_user>>.JsonToModel(res);
            if (model.State==false)
            {
                error = model.Summary;
            }
            else
            {
                error = null;
            }
            return model.Entity;

        }

    }
}
